<template>
    <header class="app-header">
      <div class="container">
        <h1 class="logo">
            <router-link to="/">小兔鲜</router-link>
      </h1>
      <AppHeaderNav></AppHeaderNav>
        <div class="search">
          <i class="iconfont icon-search"></i>
          <input ref="searchRef" type="text" placeholder="搜一搜" v-model="searchText" @keyup.enter="searchFn"/>
        </div>
        <!-- <div class="cart">
          <a class="curr" href="#">
            <i class="iconfont icon-cart"></i><em>2</em>
          </a>
        </div> -->
        <!-- 购物车 -->
        <AppHeaderCart></AppHeaderCart>
      </div>
    </header>
  </template>

<script>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import AppHeaderNav from './AppHeaderNav.vue'
import AppHeaderCart from './AppHeaderCart.vue'
export default {
  name: 'AppHeader',
  components: {
    AppHeaderNav,
    AppHeaderCart
  },
  setup () {
    const router = useRouter()
    const searchText = ref('') // 输入框的值
    const searchRef = ref(null)
    const searchFn = () => {
      //  键盘事件处理函数
      router.push(`/search?keyword=${searchText.value}`) // 跳转并传参
      searchText.value = '' // 清空输入框
      searchRef.value.blur()
    }

    return {
      searchFn,
      searchText,
      searchRef
    }
  }
}
</script>

  <style scoped lang='less'>
  .app-header {
    background: #fff;
    .container {
      display: flex;
      align-items: center;
    }
    .logo {
      width: 200px;
      a {
        display: block;
        height: 132px;
        width: 100%;
        text-indent: -9999px;
        background: url(../assets/logo.png) no-repeat center 18px / contain;
      }
    }
    .navs {
      width: 820px;
      display: flex;
      justify-content: space-around;
      padding-left: 40px;
      li {
        margin-right: 40px;
        width: 38px;
        text-align: center;
        a {
          font-size: 16px;
          line-height: 32px;
          height: 32px;
          display: inline-block;
        }
        &:hover {
          a {
            color: var(--xtx-color);
            border-bottom: 1px solid var(--xtx-color);
          }
        }
      }
    }
    .search {
      width: 170px;
      height: 32px;
      position: relative;
      border-bottom: 1px solid #e7e7e7;
      line-height: 32px;
      .icon-search {
        font-size: 18px;
        margin-left: 5px;
      }
      input {
        width: 140px;
        padding-left: 5px;
        color: #666;
      }
    }
    .cart {
      width: 50px;
      .curr {
        height: 32px;
        line-height: 32px;
        text-align: center;
        position: relative;
        display: block;
        .icon-cart {
          font-size: 22px;
        }
        em {
          font-style: normal;
          position: absolute;
          right: 0;
          top: 0;
          padding: 1px 6px;
          line-height: 1;
          background: var(--help-color);
          color: #fff;
          font-size: 12px;
          border-radius: 10px;
          font-family: Arial;
        }
      }
    }
  }
  </style>
